import json
from typing import List

from re_common.baselibrary.utils.core.mdeprecated import try_except2_async, try_except2

from apps.crawler_platform.core_callback import *
from apps.crawler_platform.core_callback.err_callback import all_client_back_callback
from apps.crawler_platform.core_platform.core_g import SQLTable
from apps.crawler_platform.core_platform.core_sql import CoreSqlMixin
from apps.crawler_platform.core_platform.g_model import DownModel, JournalArticleModel, HtmlModel, T2, T1, \
    DealItemModel, DealInsertModel, DealUpdateModel, DealItemNextModel
from apps.crawler_platform.qk_platform.base_qk_step import BaseQKStep, BaseQKItems

# 不执行逻辑，目的是为了不让 from apps.crawler_platform.core_callback import * 被编辑器认为没有使用，
# 快捷清除时就不会被清除

STATICA_HACK = False
# globals()['kcah_acitats'[::-1].upper()] = False
if STATICA_HACK:  # pragma: no cover
    null()


class ArticleParaItems(BaseQKItems):

    def __init__(self):
        super(ArticleParaItems, self).__init__()
        self.rawid: str = ''

    def get_pkm(self):
        """
        获取主键
        :return:
        """
        return {"task_name": self.task_name,
                "task_tag": self.task_tag,
                "rawid": self.rawid,
                }

    def set_downmodel(self):
        if self.task_name == "cnkijournal" and self.task_tag == "cnkiarticle":
            self.down_model = DownModel[T2](down_dict={})
        elif self.task_name == "wanfangjournal" and self.task_tag == "wanfangarticle":
            # self.down_model = DownModel[PageHtmlModel[HtmlModel]](down_dict={})
            self.down_model = DownModel[T1](down_dict={})
        else:
            self.down_model = DownModel[HtmlModel](down_dict={})


class JournalArtilceDownStep(BaseQKStep):

    def __init__(self, request, inputs):
        super().__init__()
        self.err_tag = "qkarticle"

        self.pm = ArticleParaItems()
        self.pm.set_downmodel()

        self.input = inputs
        self.pm.task_name = inputs.data.task_name
        self.pm.task_tag = inputs.data.task_tag
        self.pm.rawid = inputs.data.rawid
        self.pm.Id = inputs.data.rawid

        self.pm.redis = request.app.state.redis

        self.request = request
        self.step_info = ""
        # 解析字段字典
        self.para_dicts = {}

        self.sql_model: JournalArticleModel
        self.task_table = SQLTable.journal_article

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def get_sql(self):
        row = await CoreSqlMixin.get_article(self.pm.task_name, self.pm.task_tag,
                                             self.pm.rawid)
        if row:
            self.pm.sql_model = JournalArticleModel.parse_obj(row)
            return True, row
        else:
            return False, {
                "err_msg": "qk article sql查询不存在;" + ",".join([self.pm.task_tag, self.pm.rawid])}

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def deal_html(self):
        return await super().deal_html(JournalArticleModel)

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def save_sql(self, type="", code=""):
        """
        与关系型sql的处理 包括存储下一级的sql以及本级sql的状态和字段更新
        """
        sql_table = SQLTable.journal_article
        sql_next_table = ''
        bools, msg = await super().save_sql(sql_table, sql_next_table, type, code)
        return bools, msg
